package com.lwt.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.lwt.entity.Role;
import com.lwt.service.api.RoleService;
import com.lwt.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author Li Weitong
 * @Date 2021/1/18 16:28
 */
@Controller
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/role/get/page/info.json") // 因为返回json，扩展名写.json
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "keyword",defaultValue = "") String keyword // 获取浏览器发送过来的请求参数
         ){

            PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
            return ResultEntity.successWithData(pageInfo); // 封装到ResultEntity对象中返回json，如果抛出异常，交给异常映射机制处理

    }

    @ResponseBody
    @RequestMapping("/role/save.json")
//    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName){
    public ResultEntity<String> saveRole(Role role){
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/role/update.json")
    public ResultEntity<String> updateRole(Role role){
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/role/delete/by/role/id/array.json")
    public ResultEntity<String> removeByRoleIdArray(@RequestBody List<Integer> roleIdList){

        roleService.removeRole(roleIdList);
        System.out.println("给浏览器发送一次ajax请求");
        for (Integer integer : roleIdList) {
            System.out.println(integer);
        }
        System.out.println(roleIdList);
        return ResultEntity.successWithoutData();
    }
}
